package ui;
/**
 * EDCControlPanel
 * Class that implements the applet's control panel in which will appear differents views of DTDs and their analysis
 */
import javax.swing.*;
import javax.swing.event.*;

import utils.EDCResource;
import views.EDCHomeViewer;
import views.EDCManagerViewer;
import views.EDCParseViewer;
import views.EDCTextViewer;
import views.EDCTreeViewer;

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;

public class EDCControlPanel extends JPanel
implements ActionListener,
ListSelectionListener,
DocumentListener {

	static final long serialVersionUID = -5516482576609259711L;

	final static boolean MULTICOLORED = false;
	private Color bgColor = new Color(255, 255, 255);
	float transparency = 0.1f;
	private Image backGround = null;

	private EDCHomeViewer panel1;
	private EDCTextViewer panel2;
	private EDCTreeViewer panel3;
	private EDCManagerViewer panel4;
	private EDCParseViewer panel5;

	public EDCControlPanel() {

//		setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(600, 350));

		JTabbedPane tabbedPane = new JTabbedPane();
		ImageIcon icon = createImageIcon("images/dtdfile.gif");

		panel1 = new EDCHomeViewer();
		tabbedPane.addTab("Home", icon, panel1,
		"DTD files viewer");
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

		panel2 = new EDCTextViewer("");
		tabbedPane.addTab("DTD text viewer", icon, panel2,
		"Text viewer of a DTD file");
		tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

		panel3 = new EDCTreeViewer("");
		tabbedPane.addTab("DTD tree viewer", icon, panel3,
		"Tree viewer of a DTD file");
		tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

		panel4 = new EDCManagerViewer();
		panel4.setPreferredSize(new Dimension(410, 500));
		tabbedPane.addTab("Manager viewer", icon, panel4,
		"Manager's DTD files viewer");
		tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
		
		/*panel5 = new EDCParseViewer();
		panel5.setPreferredSize(new Dimension(410, 500));
		tabbedPane.addTab("Parser viewer", icon, panel5,
		"Parser viewer");
		tabbedPane.setMnemonicAt(4, KeyEvent.VK_5);*/
		

//		The following line enables to use scrolling tabs.
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

//		Add the tabbed pane to this panel.
		add(tabbedPane);

	}


	/** Returns an ImageIcon, or null if the path was invalid. */
	protected ImageIcon createImageIcon(String path) {
		URL imgURL=null;
		try {
			imgURL = getURL(path);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		if (imgURL != null) {
			System.out.println(imgURL);
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	//Don't allow this panel to get taller than its preferred size.
	//BoxLayout pays attention to maximum size, though most layout
	//managers don't.
	public Dimension getMaximumSize() {
		return new Dimension(Integer.MAX_VALUE,
				getPreferredSize().height);
	}


	/**
	 * 
	 */
	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();

	}

	//This method is required by ListSelectionListener.
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() == false) {

		}
	}

	//Required by DocumentListener.
	public void insertUpdate(DocumentEvent e) {
		enableButton();
	}

	//Required by DocumentListener.
	public void removeUpdate(DocumentEvent e) {
		handleEmptyTextField(e);
	}

	//Required by DocumentListener.
	public void changedUpdate(DocumentEvent e) {
		if (!handleEmptyTextField(e)) {
			enableButton();
		}
	}

	private boolean handleEmptyTextField(DocumentEvent e) {
		if (e.getDocument().getLength() <= 0) {
//			sendButton.setEnabled(false);
			return true;
		}
		return false;
	}

	private void enableButton() {
//		if (!sendButton.isEnabled()) {
//		sendButton.setEnabled(true);
//		}
	}


	//MÈthodes pour mettre une image en fond.
	public void setImageFile(JPanel p, String fichier)
	{
		setImage(loadImages( fichier ));
	}
	public void setImage( Image backGround )
	{
		this.backGround = backGround;
		setOpaque( false );
	}

	private Image loadImages( String imageFile )
	{
		try
		{
			MediaTracker mTrack = new MediaTracker( this ); // load les image avan de les afficher
			Image image = getToolkit().getImage( getURL( imageFile  ) );
			mTrack.addImage( image, 0 );
			mTrack.waitForAll();
			return image;
		}
		catch (Exception e)
		{
			System.out.println( " getimages : " + e );
		}
		return null;
	}

	public EDCHomeViewer getPanel1() {
		return panel1;
	}

	public void setPanel1(EDCHomeViewer panel1) {
		this.panel1 = panel1;
	}

	public EDCTextViewer getPanel2() {
		return panel2;
	}

	public void setPanel2(EDCTextViewer panel2) {
		this.panel2 = panel2;
	}

	public EDCTreeViewer getPanel3() {
		return panel3;
	}

	public void setPanel3(EDCTreeViewer panel3) {
		this.panel3 = panel3;
	}

	public EDCManagerViewer getPanel4() {
		return panel4;
	}

	public void setPanel4(EDCManagerViewer panel4) {
		this.panel4 = panel4;
	}

	public static URL getURL(String file) throws MalformedURLException
	{
		URL documentBase = new URL("file:///" + EDCResource.USER_DIR);
		System.out.println("img adr: " + documentBase);
		return new URL( documentBase, file);
	}

}
